import java.sql.ResultSet;
import java.sql.SQLException;


public class StationCollector implements Runnable{
	private Database db;
	
	public StationCollector(Database db) {
		this.db = db;
	}
	
	@Override
	public void run() {		
		ResultSet stationnumber = db.select("SELECT stn FROM stations");
		try {
			while (stationnumber.next()) {				
				Station station = new Station();
				int stn = stationnumber.getInt("stn");	
				if(!Server.stations.containsKey(stationnumber.getInt("stn"))) {
					ResultSet rs = db.select("SELECT * FROM measurement WHERE stn = " + stn);
					rs.last();
					int rows = rs.getRow();
					rs.first();
					if(rows > 0) {
						while(rs.next()) {
							Measurement m = new Measurement();
							m.setStn(rs.getInt(2));
							m.setDate(rs.getString(3));
							m.setTime(rs.getString(4));
							m.setTemp(rs.getFloat(5));
							m.setDewp(rs.getFloat(6));
							m.setStp(rs.getFloat(7));
							m.setSlp(rs.getFloat(8));
							m.setVisib(rs.getFloat(9));
							m.setWdsp(rs.getFloat(10));
							m.setPrcp(rs.getFloat(11));
							m.setSndp(rs.getFloat(12));
							m.setFrshtt(rs.getInt(13));
							m.setCldc(rs.getFloat(14));
							m.setWnddir(rs.getInt(15));
							station.addMeasurement(m);
						}
						System.out.println("Station recieved: " + stn + ", number of stations: " + Server.stations.size());
					}
				}
				Server.stations.put(stn, station);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	

}
